package com.taokatao.cardmee.business.profile;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taokatao.cardmee.App;
import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.common.widget.PagerSlidingTabStrip;
import com.taokatao.cardmee.db.model.Message;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ProfileMessageActivity extends BaseActivity implements View.OnClickListener, ProfileMessageLayout.Callback {
    @BindView(R.id.v_fit) View vFit;
    @BindView(R.id.i_back) View iBack;
    @BindView(R.id.v_tab_strip) PagerSlidingTabStrip vTabStrip;
    @BindView(R.id.v_pager) ViewPager vPager;
    private ProfileMessageAdapter adapter;

    @BindView(R.id.t_count_sys) TextView tCountSys;
    @BindView(R.id.t_count_business) TextView tCountBusiness;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSupportTouchBack = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_message);
        ButterKnife.bind(this);

        Utils.fitSysWindowAndSetStatusBarMode(0xFFFFFFFF, vFit);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            iBack.setBackgroundResource(R.drawable.ripple_back_black);
        }

        vPager.setAdapter(adapter = new ProfileMessageAdapter());

        vTabStrip.setViewPager(vPager);
        vTabStrip.setViewPager(vPager);
        vTabStrip.setBackgroundColor(0xFFFFFFFF);
        // 设置Tab是自动填充满屏幕的
        vTabStrip.setShouldExpand(true);
        // 设置Tab的分割线是透明的
        vTabStrip.setDividerColor(Color.TRANSPARENT);
        // 设置Tab底部线的高度
        vTabStrip.setUnderlineHeight(1);
        // 设置Tab Indicator的高度
        vTabStrip.setIndicatorHeight(App.i().d1() * 2);
        // 设置Tab标题文字的大小
        vTabStrip.setTextSize(App.i().d1() * 16);
        // 设置Tab Indicator的颜色
        vTabStrip.setIndicatorColor(0xFFFE356D);
        // 设置选中Tab文字的颜色 (这是我自定义的一个方法)
        vTabStrip.setSelectedTextColor(0xFFFE356D);
        // 取消点击Tab时的背景色
        vTabStrip.setTabBackground(0);


        vTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                //Utils.logd("vTabStrip onPageSelected ?position!" + position);
                adapter.cancelTips(position);

                if (position == 1) {
                    tCountBusiness.setVisibility(View.GONE);
                } else if (position == 0) {
                    tCountSys.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        prepareData();
    }

    private void prepareData() {
        adapter.set();
    }

    @Override
    @OnClick({R.id.i_back})
    public void onClick(View v) {
        if (v == iBack) {
            onBackPressed();
        }
    }

    @Override
    public void onDataRefresh(int messageType, int unReadCount) {
        if (messageType == Message.MESSAGE_TYPE_BUSINESS) {
            if (unReadCount > 99) unReadCount = 99;
            tCountBusiness.setText(String.valueOf(unReadCount));
            tCountBusiness.setVisibility(unReadCount > 0 ? View.VISIBLE : View.GONE);
        }
    }

    private class ProfileMessageAdapter extends PagerAdapter {
        private ProfileMessageLayout lProfileMessageBusiness, lProfileMessageSys;

        public void set() {
            notifyDataSetChanged();
        }

        public void cancelTips(int position) {
            if (position == 0) {
                if (lProfileMessageSys != null) {
                    lProfileMessageSys.removeTips();
                }
            } else if (position == 1) {
                if (lProfileMessageBusiness != null) {
                    lProfileMessageBusiness.removeTips();
                }
            }
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = container.findViewWithTag("message#" + position);
            container.removeView(view);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ProfileMessageLayout lProfileMessage = new ProfileMessageLayout(container.getContext());
            lProfileMessage.setCallback(ProfileMessageActivity.this);

            if (position == 0) {
                lProfileMessageSys = lProfileMessage;
                lProfileMessage.setMessageType(Message.MESSAGE_TYPE_SYS);

                adapter.cancelTips(0);

            } else if (position == 1) {
                lProfileMessageBusiness = lProfileMessage;
                lProfileMessage.setMessageType(Message.MESSAGE_TYPE_BUSINESS);
            }
            lProfileMessage.setTag("message#" + position);
            container.addView(lProfileMessage);
            return lProfileMessage;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return position == 0 ? "系统" : "商家";
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

}
